/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.app.Context;

import com.chinasoft_ohos.commontools.toast.Toast;
import com.yc.video.config.VideoInfoBean;
import com.yc.videocache.cache.PreloadManager;

import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.view.TikTokView;

import java.util.ArrayList;
import java.util.List;

/**
 * Tiktok2Adapter
 *
 * @since 2021-05-12
 */
public class Tiktok2Adapter extends PageSliderProvider {
    /**
     * View缓存池，从ViewPager中移除的item将会存到这里面，用来复用
     */
    private List<Component> mViewPool = new ArrayList<>();

    /**
     * 数据源
     */
    private List<VideoInfoBean> mVideoBeans;

    /**
     * 构造函数
     *
     * @param videoBeans 视频信息
     */
    public Tiktok2Adapter(List<VideoInfoBean> videoBeans) {
        this.mVideoBeans = videoBeans;
    }

    @Override
    public int getCount() {
        return mVideoBeans == null ? 0 : mVideoBeans.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer container, int position) {
        Context context = container.getContext();
        Component view = null;
        if (mViewPool.size() > 0) {
            // 取第一个进行复用
            view = mViewPool.get(0);
            mViewPool.remove(0);
        }

        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_item_tik_tok, container, false);
            viewHolder = new ViewHolder(view);
            container.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) container.getTag();
        }

        VideoInfoBean item = mVideoBeans.get(position);

        // 开始预加载
        PreloadManager.getInstance(context).addPreloadTask(item.getVideoUrl(), position);
        viewHolder.mTitle.setText(item.getTitle());
        viewHolder.mTitle.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Toast.show("点击了标题");
            }
        });
        viewHolder.mPosition = position;
        container.addComponent(view);
        return view;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer container, int position, Object object) {
        Component itemView = (Component) object;
        container.removeComponent(itemView);
        VideoInfoBean item = mVideoBeans.get(position);

        // 取消预加载
        PreloadManager.getInstance(container.getContext()).removePreloadTask(item.getVideoUrl());

        // 保存起来用来复用
        mViewPool.add(itemView);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return component == o;
    }

    /**
     * 借鉴ListView item复用方法
     *
     * @since 2021-05-12
     */
    public static class ViewHolder {
        /**
         * 角标
         */
        public int mPosition;
        /**
         * 标题
         */
        public Text mTitle;
        /**
         * 封面图
         */
        public Image mThumb;
        /**
         * TikTokView
         */
        public TikTokView mTikTokView;
        /**
         * StackLayout
         */
        public StackLayout mPlayerContainer;

        /**
         * 构造函数
         *
         * @param itemView itemView
         */
        ViewHolder(Component itemView) {
            mTikTokView = (TikTokView) itemView.findComponentById(ResourceTable.Id_tiktok_View);
            mTitle = (Text) mTikTokView.findComponentById(ResourceTable.Id_tv_title);
            mThumb = (Image) mTikTokView.findComponentById(ResourceTable.Id_iv_thumb);
            mPlayerContainer = (StackLayout) itemView.findComponentById(ResourceTable.Id_container);
            itemView.setTag(this);
        }
    }
}
